package com.shenzhen.horselive.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.Interface.OnCustomCityPickerItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.CustomCityData;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.citywheel.CustomConfig;
import com.lljjcoder.style.citycustome.CustomCityPicker;
import com.lljjcoder.style.citylist.Toast.ToastUtils;
import com.lljjcoder.style.citypickerview.CityPickerView;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.ApplyHistory;
import com.shenzhen.horselive.beans.CityAmountBean;
import com.shenzhen.horselive.beans.CityListBean;
import com.shenzhen.horselive.beans.CommResponseBean;
import com.shenzhen.horselive.databinding.ActivityApplyCityBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;
import com.shenzhen.horselive.widget.AreaPicker;

import java.util.ArrayList;
import java.util.List;

public class ApplyCityActivity extends AppBarActivity {

    ActivityApplyCityBinding  binding;
    private CustomCityPicker  customCityPicker;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding  =ActivityApplyCityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setStyleRed();
        setTitle("城市合伙人");
        initViews();
        initData();
    }

    /**
     * 获取可供选择的城市列表
     */
    List<CityListBean.DataBean>  provenceList;
    private void initData() {
        ApiFactory.getApiService().cityAreaList(User.get().getMyInfo().getToken())
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<CityListBean>(this) {
                    @Override
                    protected void onNextInActive(CityListBean cityListBean) {
                        if (cityListBean.isSuccess())
                        {
                            //省列表
                            provenceList =cityListBean.getData();
                            //造数据
                            List<CustomCityData>   customProvenceDataList = new ArrayList<>();
                            for (CityListBean.DataBean  dataBean:provenceList
                                 ) {
                                //省份
                                CustomCityData  customProvenceData = new CustomCityData();
                                customProvenceData.setId(dataBean.getAreaCode());
                                customProvenceData.setName(dataBean.getAreaName());

                                //城市
                                List<CustomCityData>  cityDataList=  new ArrayList<>();
                                for (CityListBean.DataBean.AreaListBeanX city:dataBean.getAreaList()
                                     ) {
                                    CustomCityData  cityData =  new CustomCityData();
                                    cityData.setId(city.getAreaCode());
                                    cityData.setName(city.getAreaName());

                                    //城市的区
                                    List<CustomCityData>  areaList =new ArrayList<>();
                                    for (CityListBean.DataBean.AreaListBeanX.AreaListBean areaBean:city.getAreaList()
                                         ) {
//                                        getPartnerAmount  为空的时候不可以购买
                                        if (areaBean.getPartnerAmount()!=null)
                                        {
                                        CustomCityData  areaData =  new CustomCityData();
                                        areaData.setName(areaBean.getAreaName());
                                        areaData.setId(areaBean.getAreaCode());
                                        areaList.add(areaData);
                                        }
                                    }
                                    cityData.setList(areaList);
                                    cityDataList.add(cityData);
                                }
                                 customProvenceData.setList(cityDataList);
                                customProvenceDataList.add(customProvenceData);
                            }


                            if (customProvenceDataList!=null&&customProvenceDataList.size()>0)

                            {

                                //配置设置
                                CustomConfig cityConfig = new CustomConfig.Builder()
                                        .title("选择城市")
                                        .visibleItemsCount(5)
                                        .setCityData(customProvenceDataList)//设置数据源
                                        .provinceCyclic(false)
                                        .cityCyclic(false)
                                        .districtCyclic(false)
                                        .build();

                                customCityPicker = new CustomCityPicker(getContext());
                                customCityPicker.setCustomConfig(cityConfig);
                                customCityPicker.setOnCustomCityPickerItemClickListener(new OnCustomCityPickerItemClickListener() {
                                    @Override
                                    public void onSelected(CustomCityData province, CustomCityData city, CustomCityData district) {
                                        if (province != null && city != null && district != null) {
                                            //先显示出来
                                            myProvince =province.getName();
                                            myCity = city.getName();
                                            myDistrict =  district.getName();
                                            binding.tvChooseCity.setText(province.getName()+city.getName()+district.getName());
                                            cityid  = district.getId();
                                            //获取城市价格
                                            getPrice(province.getName(),city.getName(),district.getName());

                                        }else{
                                            showToast("结果出错！");
                                        }
                                    }
                                });

                            }



                        }
                        else
                        {
                            showToast(cityListBean.getErrMessage());
                        }
                    }
                });
    }

    private void initViews() {
        binding.tvChooseCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openCityPick();
            }
        });
        binding.btGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }
    //提交信息
    private void submit() {
        if (TextUtils.isEmpty(cityid))
        {
            showToast("请选择城市");
            return;
        }
        String  name =  binding.etName.getText().toString();
        if (TextUtils.isEmpty(name))
        {
            showToast("请填写姓名");
            return;
        }
        String  phone =  binding.etPhone.getText().toString();
        if (TextUtils.isEmpty(phone))
        {
            showToast("请填写手机号码");
            return;
        }
        if (TextUtils.isEmpty(amount))
        {
            showToast("未匹配到城市价格");
            return;
        }

        ApiFactory.getApiService().applyPartner(User.get().getMyInfo().getToken()
        ,myProvince,myCity,myDistrict,phone,name,cityid,amount
        ).compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<CommResponseBean>(this) {
                    @Override
                    protected void onNextInActive(CommResponseBean commResponseBean) {
                     if (commResponseBean.isSuccess())
                     {
                         showToast("申请成功");
                         finish();
                     }else {
                         showToast(commResponseBean.getErrMessage());
                     }
                    }
                });
    }

    String  cityid;
    String  myProvince;
    String  myCity;
    String  myDistrict;
    private void openCityPick() {
        if (customCityPicker!=null)
        {
            customCityPicker.showCityPicker();
        }
        else
        {
            showToast("暂无可选城市");
        }
    }


    private   CityAmountBean  amountBean;
    private   String  amount;
    private void    getPrice(String provence,String  city,String dire)
    {
           if (provenceList==null||provenceList.size()==0)
           {
               return;
           }

        for (CityListBean.DataBean   provenceBean:provenceList
             ) {
            if (provence.equals(provenceBean.getAreaName()))
            {
                for (CityListBean.DataBean.AreaListBeanX  cityBean:provenceBean.getAreaList()
                     ) {
                    if (city.equals(cityBean.getAreaName()))
                    {
                        //区域循环
                        for (CityListBean.DataBean.AreaListBeanX.AreaListBean  areaBean:cityBean.getAreaList()
                             ) {
                            if (dire.equals(areaBean.getAreaName()))
                            {
                                //得到价格
                                binding.tvCheckPrice.setText(""+areaBean.getPartnerAmount()+"(蜂值)");
                                amount = areaBean.getPartnerAmount();
                                break;
                            }
                        }
                        break;
                    }
                }
                break;
            }
        }

    }


}